<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function get_table_comment($table)
{
    $table = Db::name($table)->getTable();
    $re = Db::query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $table . "'");
    $comments = [];
    if (is_array($re)) {
        foreach ($re as $key => $item) {
            if ($item['COLUMN_COMMENT']) {
                $comments[$item['COLUMN_NAME']] = $item;
            } else {
                $item['COLUMN_COMMENT'] = $item['COLUMN_NAME'];
                $comments[$item['COLUMN_NAME']] = $item;
            }

        }
    } else {
        return false;
    }

    return $comments;
}

//字段tinyint 1转true 0转false
function tobool(&$list, $columns)
{
    foreach ($list as $key => $item) {
        foreach ($columns as $column) {
            if ($list[$key][$column]) {
                $list[$key][$column] = true;
            } else {
                $list[$key][$column] = false;
            }
        }
    }
}

function get_curl($model, $action, $params = [])
{
    //调用登录模块，获取账号id。
    $model_api = Config("model_api.{$model}");
    //$model_api = Config('model_api.'.$model);
    $url = $model_api . "/" . $action;
    $result = \tools\curl\Curl::get($url, $params);

    if (!$result) {
        $result['status'] = false;
        $result['msg'] = '请求失败';
    } else {
        $result = \tools\curl\Curl::json2arr($result);
    }
    return $result;
}

/**
 * 记录log
 */
function logs($name, $data)
{

    file_put_contents(\think\facade\Env::get('root_path') . 'log/' . $name . "_log.txt", var_export($data, TRUE), FILE_APPEND);
    file_put_contents(\think\facade\Env::get('root_path') . 'log/' . $name . "2_log.txt", print_r($data, TRUE), FILE_APPEND);
    file_put_contents(\think\facade\Env::get('root_path') . 'log/' . $name . "_log.txt", "\r\n", FILE_APPEND);
    file_put_contents(\think\facade\Env::get('root_path') . 'log/' . $name . "2_log.txt", "\r\n", FILE_APPEND);
}